//
//  NRWCarInputItemView.m
//  New_Runway
//
//  Created by NewLife on 2018/2/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarInputItemView.h"

@implementation NRWCarInputItemView
{
    UILabel *titleLabel;
    UITextField *inputTextField;
    UIButton *manBtn;
    UIButton *womanBtn;
    
    UITextField *minPriceTextField;
    UITextField *maxPriceTextField;
}

@synthesize inputText = _inputText;
@synthesize chooseIndex = _chooseIndex;
@synthesize minPrice = _minPrice;
@synthesize maxPrice = _maxPrice;

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    
    _keyboardType = keyboardType;
    
    inputTextField.keyboardType = keyboardType;
}

- (void)setTag:(NSInteger)tag {
    
    [super setTag:tag];
    
    inputTextField.tag = tag;
}

- (void)setTitleRectOffset:(CGFloat)titleRectOffset {
    
    _titleRectOffset = titleRectOffset;
    
    [titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self.mas_left).offset(15.5 + titleRectOffset);
    }];
}

- (void)setTitleString:(NSString *)titleString {
    
    _titleString = titleString;
    
    titleLabel.text = titleString;
}

- (void)setTitleAttributedString:(NSAttributedString *)titleAttributedString {
    
    _titleAttributedString = titleAttributedString;
    
    titleLabel.attributedText = titleAttributedString;
}

- (void)setInputText:(NSString *)inputText {
    
    _inputText = inputText;
    
    inputTextField.text = inputText;
}

- (NSString *)inputText {
    
    return inputTextField.text;
}

- (void)setChooseIndex:(NSInteger)chooseIndex {
    
    _chooseIndex = chooseIndex;
    
    if (chooseIndex == 0) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
    } else if (chooseIndex == 1) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
    }
}

- (NSInteger)chooseIndex {
    
    if (manBtn.selected) {
        return 0;
    } else if (womanBtn.selected) {
        return 1;
    } else {
        return 2;
    }
}

- (instancetype)initWithInputItemWithPlaceHolder:(NSString *)placeHolder delegate:(id<UITextFieldDelegate>)delegate showArrow:(BOOL)show
{
    self = [super init];
    if (self) {
        
        [self addBaseUI];
        
        [self addInputItemWithPlaceHolder:placeHolder delegate:delegate];
        
        if (show) {
            
            [self addArrow];
        }
    }
    return self;
}

- (instancetype)initWithTwoChooseItemWithTitle1:(NSString *)title1 title2:(NSString *)title2
{
    self = [super init];
    if (self) {
        
        [self addBaseUI];
        
        [self addChooseItemWithTitle1:title1 title2:title2];
    }
    return self;
}

- (void)addArrow {
    
    UIImageView *arrowView = DPImageView().setImage(getImageFromName(@"Right"));
    self.addSubview(arrowView);
    
    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-17);
    }];
}

- (void)addBaseUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    titleLabel = DPLabel()
    .setTextColor([UIColor colorFromHexCode:@"323232"])
    .setFont(kPingFangRegular13())
    .setBackgroundColor([UIColor whiteColor]);
    
    UIView *lineView = DPView()
    .setBackgroundColor(getColorFromString(@"e1e1e1"));
    
    self.addSubview(titleLabel).addSubview(lineView);
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self.mas_left).offset(15.5);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.right.equalTo(self);
        make.height.equalTo(@0.5);
    }];
}

- (void)addInputItemWithPlaceHolder:(NSString *)placeHolder delegate:(id<UITextFieldDelegate>)delegate {
    
    inputTextField = DPTextField()
    .setFont(kPingFangRegular13())
    .setTextColor([UIColor colorFromHexCode:@"323232"])
    .setPlaceholder(placeHolder)
    .setDelegate(delegate);
    [inputTextField addTarget:self action:@selector(inputTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.addSubview(inputTextField);
    
    [inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom).offset(-0.5);
        make.left.equalTo(self.mas_left).offset(130);
        make.right.equalTo(self.mas_right).offset(-15);
    }];
}

- (void)inputTextDidChange:(UITextField *)sender {
    
    if ([self.delegate respondsToSelector:@selector(inputItemView:textFieldTextDidChange:)]) {
        
        [self.delegate inputItemView:self textFieldTextDidChange:sender];
    }
}

- (void)addChooseItemWithTitle1:(NSString *)title1 title2:(NSString *)title2 {
    
    manBtn = DPLayoutButton(5.5, DPBtnLayoutTypeLeftImageRightTitle)
    .setNormalImage(getImageFromName(@"crm_seleted_n"))
    .setSelectedImage(getImageFromName(@"crm_seleted_s"))
    .setNormalTitleColor(getColorFromString(@"323232"))
    .setSelectedTitleColor(getColorFromString(@"ff6816"))
    .addClickAction(self, @selector(genderBtnDidClick:))
    .setTitleFont(kPingFangRegular13())
    .setNormalTitle(title1);
    
    womanBtn = DPLayoutButton(5.5, DPBtnLayoutTypeLeftImageRightTitle)
    .setNormalImage(getImageFromName(@"crm_seleted_n"))
    .setSelectedImage(getImageFromName(@"crm_seleted_s"))
    .setNormalTitleColor(getColorFromString(@"323232"))
    .setSelectedTitleColor(getColorFromString(@"ff6816"))
    .addClickAction(self, @selector(genderBtnDidClick:))
    .setTitleFont(kPingFangRegular13())
    .setNormalTitle(title2);
    
    self.addSubview(manBtn).addSubview(womanBtn);
    
    [manBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(130);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom).offset(-0.5);
        make.width.equalTo(@50);
    }];
    
    [womanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(200);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom).offset(-0.5);
        make.width.equalTo(@50);
    }];
}

- (void)genderBtnDidClick:(UIButton *)sender {
    
    if (sender == manBtn) {
        
        manBtn.selected = YES;
        womanBtn.selected = NO;
    } else {
        
        manBtn.selected = NO;
        womanBtn.selected = YES;
    }
}

- (instancetype)initWithPriceRangeWithTextFieldDelegate:(id<UITextFieldDelegate>)delegate
{
    self = [super init];
    if (self) {
        
        [self addBaseUI];
        
        minPriceTextField = DPTextField()
        .setDelegate(delegate)
        .setTextAlignment(NSTextAlignmentCenter)
        .setKeyboardType(UIKeyboardTypeDecimalPad)
        .setPlaceholder(@"下限")
        .setFont(kPingFangRegular12())
        .setTextColor(getColorFromString(@"323232"));
        
        UILabel *label = DPLabel()
        .setFont(kPingFangRegular12())
        .setTextColor(getColorFromString(@"323232"))
        .setText(@"~");
        
        maxPriceTextField = DPTextField()
        .setDelegate(delegate)
        .setTextAlignment(NSTextAlignmentCenter)
        .setKeyboardType(UIKeyboardTypeDecimalPad)
        .setPlaceholder(@"上限")
        .setFont(kPingFangRegular12())
        .setTextColor(getColorFromString(@"323232"));
        
        UILabel *label2 = DPLabel()
        .setFont(kPingFangRegular12())
        .setTextColor(getColorFromString(@"323232"))
        .setText(@"万");
        
        self
        .addSubview(minPriceTextField)
        .addSubview(label)
        .addSubview(maxPriceTextField)
        .addSubview(label2);
        
        [minPriceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_right).offset(65.5);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@60);
            make.top.bottom.equalTo(self);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(minPriceTextField.mas_right).offset(8);
            make.centerY.equalTo(minPriceTextField.mas_centerY);
        }];
        
        [maxPriceTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(label.mas_right).offset(8);
            make.centerY.equalTo(label.mas_centerY);
            make.width.equalTo(@60);
            make.top.bottom.equalTo(self);
        }];
        
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(maxPriceTextField.mas_right);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
    }
    return self;
}

- (UITextField *)minPriceTextField {
    
    return minPriceTextField;
}

- (UITextField *)maxPriceTextField {
    
    return maxPriceTextField;
}

- (CGFloat)minPrice {
    
    return minPriceTextField.text.floatValue;
}

- (CGFloat)maxPrice {
    
    return maxPriceTextField.text.floatValue;
}

- (void)setMinPrice:(CGFloat)minPrice {
    
    _minPrice = minPrice;
    minPriceTextField.text = [NSString stringWithFormat:@"%.2f",minPrice];
}

- (void)setMaxPrice:(CGFloat)maxPrice {
    
    _maxPrice = maxPrice;
    maxPriceTextField.text = [NSString stringWithFormat:@"%.2f",maxPrice];
}


@end
